package cn.ainit.sys.ui.controller;

import java.util.List;


import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ainit.sys.common.dao.SysBaseDao;
import cn.ainit.sys.ds.cloumns.sysCloumns;
import cn.ainit.sys.framework.entity.SysButtons;
import cn.ainit.sys.framework.entity.SysColumns;
import cn.ainit.sys.pages.config.entity.ConfigFunctions;
import cn.ainit.sys.pages.config.entity.ConfigObject;
import cn.ainit.sys.query.datastore.DataStore;
import cn.ainit.sys.query.entity.QueryButton;
import cn.ainit.sys.query.entity.QueryColumn;
import cn.ainit.sys.ui.buttons.FormButtons;
import cn.ainit.sys.ui.entity.UIObject;
import cn.ainit.sys.ui.forms.Columns;

@Transactional
@Repository("uiDao")
public class UIDaoImpl extends SysBaseDao implements UIDao{

	@Override
	public List<Columns> getColumnsById(String dsid) throws Exception {
		String hql = "from Columns cln where cln.dsid = ? order by cln.groupId,cln.parentGroup,cln.orderNum";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, dsid);
		return (List<Columns>)query.list();
	}
	
	@Override
	public List<FormButtons> findAllButtons(String formType) throws Exception {
		String hql = "from FormButtons btn where btn.show = 'T' and btn.dsid = ? order by btn.orderNum";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, formType);
		return (List<FormButtons>)query.list();
	}
	@Override
	public List<QueryButton> getQueryButns(String dsId) throws Exception {
		String hql = "from QueryButton btn where btn.show = 'T' and btn.dsid = ? order by btn.orderNum";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, dsId);
		return (List<QueryButton>)query.list();
	}
	@Override
	public List<String> getSameInfoStr(String tableName, String columnName, String str)
			throws Exception {
		String hql = "select "+columnName+" from "+ tableName + " where " + columnName +" like '%"+str+"%' order by "+columnName;
		Query query = sessionFactory.getCurrentSession().createSQLQuery(hql);
		query.setMaxResults(50);
		return (List<String>)query.list();
	}

	@Override
	public boolean saveColumn(Columns columns) throws Exception {
		try {
			sessionFactory.getCurrentSession().save(columns);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean addButns(SysButtons butns) throws Exception {
		try {
			sessionFactory.getCurrentSession().save(butns);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	@Override
	public boolean addQueryButns(QueryButton queryButton) throws Exception {
		try {
			sessionFactory.getCurrentSession().save(queryButton);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	@Override
	public boolean saveDS(DataStore dataStore) throws Exception {
		try {
			sessionFactory.getCurrentSession().save(dataStore);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}	
		return false;
	}
	@Override
	public boolean saveQueryCol(List<String> list,String dsid) throws Exception {
		try {
			Session session = this.getSessionFactory().openSession();
			Transaction tx = session.beginTransaction();
			for (int i = 0; i < list.size(); i++) {
				SysColumns queryColumn = new SysColumns();
					queryColumn.setDsid(dsid);
					queryColumn.setName(list.get(i));
					queryColumn.setColnName(list.get(i));
					queryColumn.setIsshow("T");
					queryColumn.setFirstpageflag("T");
					session.save(queryColumn);
			}
			session.flush();
			tx.commit();
			session.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	@Override
	public boolean saveQueryUICol(List<String> list, String dsid)
			throws Exception {
		try {
			Session session = this.getSessionFactory().openSession();
			Transaction tx = session.beginTransaction();
			for (int i = 0; i < list.size(); i++) {
				Columns column = new Columns();
				column.setDsid(dsid);
				column.setName(list.get(i));
				column.setColnName(list.get(i));
				session.save(column);
			}
			session.flush();
			tx.commit();
			session.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	@Override
	public List<String> callBackSql(String sqlText) throws Exception {
		return this.callBackCol(sqlText);
	}
	
	@Override
	public List<SysColumns> queryColumns(String dsId) throws Exception {
		try {
			String hql = "from SysColumns q where q.dsid=? and q.show='T' order by q.orderNum ";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, dsId);
			List<SysColumns> list = query.list();
			if(list != null && list.size() > 0){
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public UIObject findFormObjById(String objId, String formObjId) throws Exception {
		UIObject uiObject = null;
		try {
			String hql = " from UIObject uo where uo.objId = '"+objId+"' and uo.typeId = '"+formObjId+"'";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			List<UIObject> list = query.list();
			if(list != null && list.size() > 0)uiObject = list.get(0);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return uiObject;
	}
	
	@Override
	public DataStore getDataStoreById(String dsid) throws Exception {
		String hql = " from DataStore ds where ds.dsId = ?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, dsid);
		List<DataStore> list = query.list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public Object querySelectOptions(String sqlText,String config,
			String showPK, String showName, Integer pageRows) throws Exception {
		Object object = null;
		try {
			String sql = " select "+showPK+" id,"+showName+" name from ("+sqlText+" "+config+") TMP";
			object = this.getJdbcTemplate().queryForList(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}
	 @Override
	public Object queryColBySql(String sqlText) throws Exception {
		 Object objects = null;
		 try {
//			 Pagination pagination = new Pagination(dbType, sqlText, 1, 500, this.getJdbcTemplate());
			 SQLQuery query = this.getSessionFactory().getCurrentSession().createSQLQuery(sqlText);
			 objects = query.list();
			 objects = this.getJdbcTemplate().queryForList(sqlText);
//			 objects = pagination.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return objects;
	}
	 @Override
	public boolean updQueryCol(String sqlTex) throws Exception {
		 try {
			this.executeSQL(sqlTex);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return false;
	}
	@Override
	public ConfigObject findConfigObj(String configId) throws Exception {
		String hql = "from ConfigObject cfo where cfo.configId = ?";
		Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
		query.setString(0, configId);
		List list = query.list();
		ConfigObject cfobj = new ConfigObject();
		if (list!= null && list.size() > 0) {
			cfobj = (ConfigObject) list.get(0);
		}
		return cfobj;
	}
	
	@Override
	public ConfigFunctions findConfigFunsById(String funsId) throws Exception {
		ConfigFunctions configFunctions = null;
		String hql = "from ConfigFunctions cf where cf.funcId = ?";
		Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
		query.setString(0, funsId);
		List list = query.list();
		if (list!= null && list.size() > 0) {
			configFunctions = (ConfigFunctions) list.get(0);
		}
		return configFunctions;
	}
	@Override
	public boolean upQueryColn(Columns columns) throws Exception {
		try {
			this.getSessionFactory().getCurrentSession().update(columns);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	@Override
	public boolean upQuCol(QueryColumn queryColumn) throws Exception {
		try {
			this.getSessionFactory().getCurrentSession().update(queryColumn);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	@Override
	public boolean updateObject(String sqlText, String[] valuesArr,
			String[] whereValue) throws Exception {
		return this.executeUpdateSql(sqlText, valuesArr, whereValue);
	}
}
